home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / read_ppm.pro < prev    next >
Text File  |  1997-07-08  |  3KB  |  130 lines

  1. ; $Id: read_ppm.pro,v 1.4 1997/04/05 00:29:35 kirk Exp $
  2. ;
  3. ; Copyright (c) 1994-1997. Research Systems, Inc. All rights reserved.
  4. ;    Unauthorized reproduction prohibited.
  5. ;
  6. Function READ_PPM_NEXT_LINE, unit
  7. cr = string(13b)
  8. repeat begin
  9.     a = ''
  10.     readf, unit, a
  11.     l = strpos(a, '#')   ;Strip comments
  12.     if l ge 0 then a = strmid(a,0,l)
  13.     if strmid(a,0,1) eq cr then a = strmid(a,1,1000)  ;Leading <CR>
  14.     if strmid(a,strlen(a)-1,1) eq cr then a = strmid(a,0,strlen(a)-1)
  15.     a = strtrim(a,2)        ;Remove leading & trailing blanks.
  16.     a = strcompress(a)        ;Compress white space.
  17. endrep until strlen(a) gt 0
  18. return, a
  19. end
  20.  
  21. Function READ_PPM_NEXT_TOKEN, unit, buffer
  22. if strlen(buffer) le 0 then buffer = READ_PPM_NEXT_LINE(unit)
  23. white = strpos(buffer, ' ')
  24. if white eq -1 then begin    ;No blanks?
  25.     result = buffer
  26.     buffer = ''
  27. endif else begin        ;Strip leading token.
  28.     result = strmid(buffer, 0, white)
  29.     buffer = strmid(buffer, white+1, 1000)
  30. endelse
  31. return, result
  32. end
  33.  
  34.  
  35.  
  36. PRO READ_PPM, FILE, IMAGE, MAXVAL = maxval
  37. ;
  38. ;+
  39. ; NAME:
  40. ;    READ_PPM
  41. ;
  42. ; PURPOSE:
  43. ;    Read the contents of a PGM (gray scale) or PPM (portable pixmap
  44. ;    for color) format image file and return the image in the form
  45. ;    of an IDL variable.
  46. ;    PPM/PGM format is supported by the PMBPLUS and Netpbm packages.
  47. ;
  48. ;    PBMPLUS is a toolkit for converting various image formats to and from
  49. ;    portable formats, and therefore to and from each other.
  50. ;
  51. ; CATEGORY:
  52. ;    Input/Output.
  53. ;
  54. ; CALLING SEQUENCE:
  55. ;    READ_PPM, File, Image
  56. ;
  57. ; INPUTS:
  58. ;    File:    Scalar string giving the name of the PGM or PPM file.
  59. ;
  60. ; OUTPUTS:
  61. ;    Image:    The 2D byte array to contain the image.  In the case
  62. ;        of a PPM file, a [3, n, m] array is returned.
  63. ;
  64. ; KEYWORD Parameters:
  65. ;    MAXVAL = returned maximum pixel value.
  66. ; SIDE EFFECTS:
  67. ;    None.
  68. ; RESTRICTIONS:
  69. ;    Should adhere to the PGM/PPM "standard".
  70. ;    Accepts: P2 = graymap ASCII, P5 graymap RAWBITS, P3 true-color
  71. ;    ASCII pixmaps, and P6 true-color RAWBITS pixmaps.
  72. ;    Maximum pixel values are limited to 255.
  73. ;    Images are always stored with the top row first. (ORDER=1)
  74. ;
  75. ; EXAMPLE:
  76. ;    To open and read the PGM image file named "foo.pgm" in the current
  77. ;    directory, store the image in the variable IMAGE1 enter:
  78. ;
  79. ;        READ_PPM, "foo.gif", IMAGE1
  80. ;
  81. ; MODIFICATION HISTORY:
  82. ;    Written Nov, 1994, DMS.
  83. ;-
  84. ; Copyright (c) 1994, Research Systems, Inc.  All rights reserved.
  85. ;    Unauthorized reproduction prohibited.
  86. ;
  87.  
  88. ON_IOERROR, bad_io
  89. ON_ERROR, 1
  90.  
  91. OPENR, unit, file, /GET_LUN, /binary, /noautomode, /BLOCK
  92. image = 0
  93. buffer = ''        ;Read using strings
  94. magic = READ_PPM_NEXT_TOKEN(unit, buffer)
  95. if strmid(magic,0,1) ne 'P' then begin
  96. Not_pgm: MESSAGE, 'File "'+file+'" is not a PGM/PPM file.'
  97.     return
  98.     endif
  99.  
  100. type = strmid(magic,1,1)
  101.  
  102. width = long(READ_PPM_NEXT_TOKEN(unit, buffer))
  103. height = long(READ_PPM_NEXT_TOKEN(unit, buffer))
  104. maxval = long(READ_PPM_NEXT_TOKEN(unit, buffer))
  105. case type of
  106. '2' : BEGIN
  107.     image = bytarr(width, height, /nozero)
  108.     readf, unit, image
  109.       ENDCASE
  110. '3' : BEGIN
  111.     image = bytarr(3, width, height, /nozero)
  112.     readf, unit, image
  113.       ENDCASE
  114. '5' : BEGIN
  115.     image = bytarr(width, height, /nozero)
  116.     readu, unit, image
  117.       ENDCASE
  118. '6' : BEGIN
  119.     image = bytarr(3, width, height, /nozero)
  120.     readu, unit, image
  121.       ENDCASE
  122. else :    goto, Not_pgm
  123. ENDCASE
  124.  
  125. free_lun, unit
  126. return
  127. BAD_IO: Message, 'Error occured accessing PGM/PPM file:' + file
  128. end
  129.  
  130.